﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
namespace Ebada.Modules.Vehicle
{
    /// <summary>
    /// 违章记录编辑
    /// </summary>
    public partial class frmVehicleGasCardEdit : FormBase, IPopupFormEdit {
       

        public frmVehicleGasCardEdit() {
            InitializeComponent();
        }
        void dataBind() {
            this.txtGasCard_Num.DataBindings.Add("EditValue", rowData, "GasCard_Num");
            this.txtStartDate.DataBindings.Add("EditValue", rowData, "StartDate");
            this.txtOrgCode.DataBindings.Add("EditValue", rowData, "OrgCode");
            this.txtOrgCnName.DataBindings.Add("EditValue", rowData, "OrgCnName");
            this.txtTatalPrice.DataBindings.Add("EditValue", rowData, "TatalPrice");
            this.txtEndDate.DataBindings.Add("EditValue", rowData, "EndDate");
            this.txtRemark.DataBindings.Add("EditValue", rowData, "Remark");
            
        }
        #region IPopupFormEdit Members
        private VehicleGasCard rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null) return;
                if (rowData == null) {
                    this.rowData = value as VehicleGasCard;
                    dataBind();
                    InitComboBoxData();
                } else {
                    ConvertHelper.CopyTo<VehicleGasCard>(value as VehicleGasCard, rowData);
                    InitComboBoxData();
                }
            }
        }

        #endregion

        private void InitComboBoxData() {
            //IList<Activities> list= MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' order by Act_ID");
            //txtAct_Name.Properties.DataSource = list;
            if (rowData.GasCard_Num == "")
            {
                rowData.StartDate = DateTime.Now;
                rowData.EndDate = DateTime.Now.AddDays(90);
            }
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtGasCard_Num.Text == "")
            {
                MessageBox.Show("油卡卡号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtGasCard_Num.Focus();
                return;
            }
            if (txtOrgCnName.Text == "")
            {
                MessageBox.Show("办理单位名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtOrgCnName.Focus();
                return;
            }
            IList<VehicleGasCard> objlist = MainHelper.GetSqlMap<VehicleGasCard>().GetListByWhere<VehicleGasCard>(" where GasCard_Num='" + txtGasCard_Num.Text + "'");
            if (objlist.Count > 0)
            {
                if (rowData.GasCard_ID != objlist[0].GasCard_ID)
                {
                    MessageBox.Show("此加油卡号已经存在。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtOrgCnName_Click(object sender, EventArgs e)
        {
            OrgClick();
        }

        private void txtOrgCnName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OrgClick();
        }
        public void OrgClick()
        {
            frmOrgSelector2 frm = new frmOrgSelector2();
            frm.IntionDataAll();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                rowData.OrgCnName = frm.SelectObj.cnName;
                txtOrgCnName.Text = frm.SelectObj.cnName;
                rowData.OrgCode = frm.SelectObj.CityCode;
                txtOrgCode.Text = frm.SelectObj.CityCode;
            }
        }

    }
}